//
//  LoginViewController.m
//  智慧小区
//
//  Created by lab603 on 15/6/19.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "LoginViewController.h"
#import "ActionSheetStringPicker.h"
#import "APIClient.h"
#import "PPHttpTool.h"
#import "MJExtension.h"
#import "Community.h"
#import "MBProgressHUD+MJ.h"
#import "PPTabBarController.h"
#import "EGOCache.h"
#import "UserInfo.h"
#import "PPTabBarController.h"
#import "RegisterViewController.h"
#import "Tool.h"
#define TOP_PADDING 10.f
#define BOTTOM_PADDING 10.f
#define LEFT_PADDING 20.f
#define RIGHT_PADDING 20.f
#define Cache_UserInfoKey @"UserInfoCache"
#define CacheTime 24*60*60
@interface LoginViewController ()<UIGestureRecognizerDelegate>
//背景
@property (nonatomic,weak)UIImageView *backgroundView;
//提示Label
@property (nonatomic,weak)UILabel *tipOneLabel;

//选择小区
@property (nonatomic,weak)UIView *communityView;
//选择小区Label
@property (nonatomic,weak)UILabel *communityLabel;
//选择label
@property (nonatomic,weak)UILabel *chooseCommunity;
//小剪头
@property (nonatomic,weak)UIImageView *arrowView;

//帐号
@property (nonatomic,weak)UIView *accountView;
//帐号Label
@property (nonatomic,weak)UILabel *accountLabel;
//帐号TextField
@property (nonatomic,weak)UITextField *accountField;

//密码View
@property (nonatomic,weak)UIView *passwordView;
//密码Label
@property (nonatomic,weak)UILabel *passwordLabel;
//密码TextField
@property (nonatomic,weak)UITextField *passwordField;

//忘记密码
@property (nonatomic,weak)UILabel *forgetPassword;
//记住密码
@property (nonatomic,weak)UIButton *rememberBtn;
//登录Button
@property (nonatomic,weak)UIButton *loginButton;
//快速注册
@property (nonatomic,weak)UIButton *registerBtn;
//tip2
@property (nonatomic,weak)UILabel *tipTwoLabel;
//tip3
@property (nonatomic,weak)UILabel *tipThreeLabel;

//保存小区的数组
@property (nonatomic,strong)NSArray *communityArray;
//保存小区的代号
@property (nonatomic,copy)NSString *dbName;
//保存小区的名称
@property (nonatomic,copy)NSString *communityName;
//保存是否记住密码
@property (nonatomic,assign)BOOL isRemember;
//保存用户信息对象
@property (nonatomic,strong)UserInfo *userInfo;
@end

@implementation LoginViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    //默认是保存状态
    self.isRemember = YES;
    //添加控件
    [self addSubview];
    //设置属性
    [self setLayout];
    //设置用户数据
    [self setUserData];
    //设置view的背景色
    self.view.backgroundColor = IWColor(235, 235, 235);
}
/**
 *  设置用户数据
 */
- (void)setUserData{
    if (![[EGOCache globalCache] hasCacheForKey:Cache_UserInfoKey]) return;
    self.chooseCommunity.textColor = [UIColor blackColor];
    UserInfo *userInfo = (UserInfo *)[[EGOCache globalCache] objectForKey:Cache_UserInfoKey];
    self.chooseCommunity.text = userInfo.community;
    self.accountField.text = userInfo.account;
    self.isRemember = userInfo.isRemember;
    if (self.isRemember) {
        self.passwordField.text = userInfo.password;
        self.rememberBtn.selected = YES;
    }else{
        self.passwordField.text = @"";
        self.rememberBtn.selected = NO;
    }
    self.dbName = userInfo.dbName;
    self.communityName = userInfo.community;
}
/**
 *  添加子控件
 */
- (void)addSubview{
    //背景
    UIImageView *backgroundView = [[UIImageView alloc] init];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    self.backgroundView = backgroundView;
    //提示1
    UILabel *tipOneLabel = [[UILabel alloc] init];
    tipOneLabel.textColor=[UIColor grayColor];
    tipOneLabel.text=@"欢迎使用Hi小区应用";
    [tipOneLabel setFont:[UIFont systemFontOfSize:12.0]];
    [tipOneLabel sizeToFit];
    [self.backgroundView addSubview: tipOneLabel];
    self.tipOneLabel = tipOneLabel;
    
    //小区View
    UIView *communityView=[[UIView alloc]init];
    communityView.backgroundColor=[UIColor whiteColor];
    communityView.layer.borderWidth = 0.5;
    communityView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [communityView.layer setCornerRadius:CORNER_RADIUS_8];
    [self.backgroundView addSubview:communityView];
    self.communityView = communityView;
    //小区Label
    UILabel *communityLabel = [[UILabel alloc] init];
    communityLabel.textColor=APP_WORD_COLOR_1;
    communityLabel.text=@"小区";
    [communityLabel setFont:[UIFont systemFontOfSize:14.4]];
    [communityLabel sizeToFit];
    [self.communityView addSubview: communityLabel];
    self.communityLabel = communityLabel;
    //选择label
    UILabel *chooseCommunity = [[UILabel alloc] init];
    chooseCommunity.textColor=APP_THEME_COLOR;
    chooseCommunity.text=@"请选择小区(点我)";
    [chooseCommunity setFont:[UIFont systemFontOfSize:16.0]];
    [chooseCommunity sizeToFit];
    //添加点击手势
    chooseCommunity.userInteractionEnabled = YES;
    [chooseCommunity addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseCommunityTapped)]];
    [self.communityView addSubview: chooseCommunity];
    self.chooseCommunity = chooseCommunity;
    //小剪头
    UIImageView *arrowView = [[UIImageView alloc] init];
    arrowView.image = [UIImage imageNamed:@"arrows_down_icon"];
    [self.communityView addSubview:arrowView];
    self.arrowView = arrowView;

    //帐号View
    UIView *accountView=[[UIView alloc]init];
    accountView.backgroundColor=[UIColor whiteColor];
    accountView.layer.borderWidth = 0.5;
    accountView.layer.borderColor = [APP_HINT_COLOR CGColor];
    [accountView.layer setCornerRadius:CORNER_RADIUS_8];
    [self.backgroundView addSubview:accountView];
    self.accountView = accountView;
    //帐号label
    UILabel *accountLabel = [[UILabel alloc] init];
    accountLabel.textColor=APP_WORD_COLOR_1;
    accountLabel.text=@"帐号";
    [accountLabel setFont:[UIFont systemFontOfSize:14.4]];
    [accountLabel sizeToFit];
    [self.accountView addSubview: accountLabel];
    self.accountLabel = accountLabel;
    //帐号field
    UITextField *accountField = [[UITextField alloc] init];
    accountField.placeholder = @"请输入帐号";
    accountField.textColor = RGBACOLOR(86,84,84,1);
    accountField.returnKeyType = UIReturnKeyDone;
    //accountField.keyboardType = UIKeyboardTypePhonePad;
    accountField.enablesReturnKeyAutomatically = YES;
    accountField.clearButtonMode = UITextFieldViewModeAlways;
    [accountField addTarget:self action:@selector(hidenKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.accountView addSubview:accountField];
    self.accountField = accountField;
    
    //密码View
    UIView *passwordView=[[UIView alloc]init];
    passwordView.backgroundColor=[UIColor whiteColor];
    passwordView.layer.borderWidth = 0.5;
    passwordView.layer.borderColor = [APP_HINT_COLOR CGColor];
    [passwordView.layer setCornerRadius:CORNER_RADIUS_8];
    [self.backgroundView addSubview:passwordView];
    self.passwordView = passwordView;
    //密码label
    UILabel *passwordLabel = [[UILabel alloc] init];
    passwordLabel.textColor=APP_WORD_COLOR_1;
    passwordLabel.text=@"密码";
    [passwordLabel setFont:[UIFont systemFontOfSize:14.4]];
    [passwordLabel sizeToFit];
    [self.passwordView addSubview: passwordLabel];
    self.passwordLabel = passwordLabel;
    //密码field
    UITextField *passwordField = [[UITextField alloc] init];
    passwordField.placeholder = @"请输入密码";
    passwordField.textColor = RGBACOLOR(86,84,84,1);
    passwordField.secureTextEntry = YES;
    passwordField.returnKeyType = UIReturnKeyDone;
    //passwordField.keyboardType = UIKeyboardTypePhonePad;
    passwordField.enablesReturnKeyAutomatically = YES;
    passwordField.clearButtonMode = UITextFieldViewModeAlways;
    [passwordField addTarget:self action:@selector(hidenKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.passwordView addSubview:passwordField];
    self.passwordField = passwordField;

    //忘记密码
    UILabel *forgetPassword = [[UILabel alloc] init];
    forgetPassword.textColor=[UIColor grayColor];
    forgetPassword.text=@"忘记密码？";
    [forgetPassword setFont:[UIFont systemFontOfSize:14.0]];
    [forgetPassword sizeToFit];
    //添加点击手势
    forgetPassword.userInteractionEnabled = YES;
    [forgetPassword addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(forgetPassword:)]];
    [self.backgroundView addSubview: forgetPassword];
    self.forgetPassword = forgetPassword;
    
    //记住密码
    UIButton *rememberBtn = [[UIButton alloc] init];
    rememberBtn.selected = YES;
    [rememberBtn setTitle:@"记住密码" forState:UIControlStateNormal];
    [rememberBtn setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [rememberBtn setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
    [rememberBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    rememberBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rememberBtn addTarget:self action:@selector(rememberBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    rememberBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [self.backgroundView addSubview:rememberBtn];
    self.rememberBtn = rememberBtn;
    
    //登录按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginButton.layer.borderWidth = 1;
    loginButton.layer.borderColor = [APP_HINT_COLOR CGColor];
    [loginButton.layer setCornerRadius:CORNER_RADIUS_8];
    loginButton.backgroundColor=APP_THEME_COLOR;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview: loginButton];
    self.loginButton = loginButton;
    //快速注册
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setTitleColor:APP_THEME_COLOR forState:UIControlStateNormal];
    registerBtn.layer.borderWidth = 1;
    registerBtn.layer.borderColor = [APP_HINT_COLOR CGColor];
    [registerBtn.layer setCornerRadius:CORNER_RADIUS_8];
    registerBtn.backgroundColor=[UIColor whiteColor];
    [registerBtn setTitle:@"快速注册" forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [registerBtn addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview: registerBtn];
    self.registerBtn = registerBtn;

    //tip2
    UILabel *tipTwoLabel = [[UILabel alloc] init];
    tipTwoLabel.textColor=APP_WORD_COLOR_2;
    tipTwoLabel.text=@"注册表示已阅读并同意";
    [tipTwoLabel setFont:[UIFont systemFontOfSize:9.6]];
    [tipTwoLabel sizeToFit];
    [self.backgroundView addSubview: tipTwoLabel];
    self.tipTwoLabel = tipTwoLabel;
    
    //tip3
    UILabel *tipThreeLabel = [[UILabel alloc] init];
    tipThreeLabel.textColor=APP_THEME_COLOR;
    tipThreeLabel.text=@"用户服务协议";
    [tipThreeLabel setFont:[UIFont systemFontOfSize:9.6]];
    [tipThreeLabel sizeToFit];
    [self.backgroundView addSubview: tipThreeLabel];
    [tipThreeLabel setUserInteractionEnabled:YES];
    [tipThreeLabel addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showProtocol)]];
    
    self.tipThreeLabel = tipThreeLabel;
    
    //添加手势，点击屏幕其他区域关闭键盘的操作
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
    gesture.numberOfTapsRequired = 1;
    gesture.delegate = self;
    [self.view addGestureRecognizer:gesture];
}
/**
 *  设置子控件
 */
- (void)setLayout{
    //设置frame
    CGFloat VHeight = 35.f;
    //背景
    CGFloat backgroundX = 0;
    CGFloat backgroundY = NAVIGATION_HEIGHT + STATUSBAR_HEIGHT;
    CGFloat backgroundW = ScreenWidth;
    //高度待定
    CGFloat backgroundH = 0;
    //提示Label
    CGFloat tipOneLabelX = LEFT_PADDING;
    CGFloat tipOneLabelY = TOP_PADDING;
    CGFloat tipOneLabelW = self.tipOneLabel.frame.size.width;
    CGFloat tipOneLabelH = self.tipOneLabel.frame.size.height;
    _tipOneLabel.frame = CGRectMake(tipOneLabelX, tipOneLabelY, tipOneLabelW, tipOneLabelH);
    
    //小区view
    CGFloat communityViewX = LEFT_PADDING;
    CGFloat communityViewY = CGRectGetMaxY(_tipOneLabel.frame)+TOP_PADDING;
    CGFloat communityViewW = ScreenWidth - LEFT_PADDING*2;
    CGFloat communityViewH = VHeight;
    _communityView.frame = CGRectMake(communityViewX, communityViewY, communityViewW, communityViewH);
    //小区Label
    CGFloat communityLabelX = TOP_PADDING;
    CGFloat communityLabelY = TOP_PADDING;
    _communityLabel.frame = (CGRect){{communityLabelX,communityLabelY},self.communityLabel.bounds.size};
    //选择小区
    CGFloat chooseCommunityX = 85;
    CGFloat chooseCommunityY = 0;
    CGFloat chooseCommunityW = 180;
    CGFloat chooseCommunityH = VHeight;
    _chooseCommunity.frame = (CGRect)CGRectMake(chooseCommunityX, chooseCommunityY, chooseCommunityW, chooseCommunityH);
    //小箭头
    CGSize arrowViewSize = CGSizeMake(20, 20);
    CGFloat arrowViewX = communityViewW - TOP_PADDING - arrowViewSize.width;
    CGFloat arrowViewY = (VHeight - arrowViewSize.height)*0.5;
    _arrowView.frame = (CGRect){{arrowViewX,arrowViewY},arrowViewSize};

    //帐号View
    CGFloat accountX = communityViewX;
    CGFloat accountY = CGRectGetMaxY(_communityView.frame)+TOP_PADDING;
    CGFloat accountW = communityViewW;
    CGFloat accountH = VHeight;
    _accountView.frame = CGRectMake(accountX, accountY, accountW, accountH);
    //帐号Label
    CGFloat accountLabelX = TOP_PADDING;
    CGFloat accountLabelY = TOP_PADDING;
    _accountLabel.frame = (CGRect){{accountLabelX,accountLabelY},self.accountLabel.bounds.size};
    //帐号TextField
    CGFloat accountFieldX = CGRectGetMaxX(_accountLabel.frame)+TOP_PADDING;
    CGFloat accountFieldY = 0;
    CGFloat accountFieldW = accountW - accountFieldX;
    CGFloat accountFieldH = VHeight;
    _accountField.frame = CGRectMake(accountFieldX, accountFieldY, accountFieldW, accountFieldH);
    
    //密码View
    CGFloat passwordViewX = accountX;
    CGFloat passwordViewY = CGRectGetMaxY(_accountView.frame)+TOP_PADDING;
    CGFloat passwordViewW = communityViewW;
    CGFloat passwordViewH = VHeight;
    _passwordView.frame = CGRectMake(passwordViewX, passwordViewY, passwordViewW, passwordViewH);
    //密码Label
    CGFloat passwordLabelX = TOP_PADDING;
    CGFloat passwordLabelY = TOP_PADDING;
    _passwordLabel.frame = (CGRect){{passwordLabelX,passwordLabelY},self.passwordLabel.bounds.size};
    //密码TextField
    CGFloat passwordFieldX = CGRectGetMaxX(_passwordLabel.frame)+TOP_PADDING;
    CGFloat passwordFieldY = 0;
    CGFloat passwordFieldW = passwordViewW - passwordFieldX;
    CGFloat passwordFieldH = VHeight;
    _passwordField.frame = CGRectMake(passwordFieldX, passwordFieldY, passwordFieldW, passwordFieldH);

    //记住密码
    CGSize rememberBtnSize = (CGSize){90,25};
    CGFloat rememberBtnX = LEFT_PADDING;
    CGFloat rememberBtnY = CGRectGetMaxY(_passwordView.frame)+TOP_PADDING*0.5;
    _rememberBtn.frame = (CGRect){{rememberBtnX,rememberBtnY},rememberBtnSize};
    
    //忘记密码
    CGFloat forgetPasswordX = passwordViewW - self.forgetPassword.bounds.size.width+LEFT_PADDING;
    CGFloat forgetpasswordY = CGRectGetMaxY(_passwordView.frame)+TOP_PADDING;
    _forgetPassword.frame = (CGRect){{forgetPasswordX,forgetpasswordY},self.forgetPassword.bounds.size};
    
    //快速注册按钮
    CGFloat registerBtnX = LEFT_PADDING;
    CGFloat registerBtnY = CGRectGetMaxY(_rememberBtn.frame)+TOP_PADDING*0.5;
    CGFloat registerBtnW = (ScreenWidth - LEFT_PADDING*2 -10)*0.5;
    CGFloat registerBtnH = 30;
    _registerBtn.frame = CGRectMake(registerBtnX, registerBtnY, registerBtnW, registerBtnH);
    //下一步按钮
    CGFloat loginButtonX = CGRectGetMaxX(_registerBtn.frame)+TOP_PADDING;
    CGFloat loginButtonY = CGRectGetMaxY(_rememberBtn.frame)+TOP_PADDING*0.5;
    CGFloat loginButtonW = (ScreenWidth - LEFT_PADDING*2 -10)*0.5;
    CGFloat loginButtonH = 30;
    _loginButton.frame = CGRectMake(loginButtonX, loginButtonY, loginButtonW, loginButtonH);
    //tip2
    CGFloat tipTwoLabelX = registerBtnX;
    CGFloat tipTwoLabelY = CGRectGetMaxY(_loginButton.frame)+TOP_PADDING;
    _tipTwoLabel.frame = (CGRect){{tipTwoLabelX,tipTwoLabelY},self.tipTwoLabel.frame.size};
    //tip3
    CGFloat tipThreeLabelX = CGRectGetMaxX(_tipTwoLabel.frame);
    CGFloat tipThreeLabelY = tipTwoLabelY;
    _tipThreeLabel.frame = (CGRect){{tipThreeLabelX,tipThreeLabelY},self.tipThreeLabel.frame.size};
    backgroundH = CGRectGetMaxY(_tipThreeLabel.frame)+TOP_PADDING;
    _backgroundView.frame = CGRectMake(backgroundX, backgroundY, backgroundW, backgroundH);

}
/**
 *  点击了记住密码按钮
 */
- (void)rememberBtnClick:(UIButton *)rememberBtn
{
    rememberBtn.selected = !rememberBtn.isSelected;
    self.isRemember = rememberBtn.selected;
}
/**
 *  注册
 */
- (void)registerAction{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}
/**
 *  忘记密码
 */
- (void)forgetPassword:(UILabel *)forgetLabel{
    
}
/**
 *  选择小区
 */
- (void)chooseCommunityTapped{
    NSString *urlString = [APIClient relativePathForCommunity];
    IWLog(@"%@",urlString);
    NSMutableArray *tempArray = [NSMutableArray array];
    [PPHttpTool getWithURL:urlString params:nil success:^(id json) {
        IWLog(@"%@",json);
        self.communityArray = [Community objectArrayWithKeyValuesArray:json];
        //得到要展示的小区数组
        for (int i = 0; i<self.communityArray.count; i++) {
            Community *community = self.communityArray[i];
            [tempArray addObject:community.communityName];
        }
        //创建pickerView
        [ActionSheetStringPicker showPickerWithTitle:@"请选择小区" rows:tempArray initialSelection:0 doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
            IWLog(@"%ld,%@",(long)selectedIndex,selectedValue);
            self.chooseCommunity.textColor = [UIColor blackColor];
            self.chooseCommunity.text = selectedValue;
            Community *community = self.communityArray[selectedIndex];
            self.dbName = community.dbName;
            self.communityName = community.communityName;
        } cancelBlock:^(ActionSheetStringPicker *picker) {
            
        } origin:self.view];
    } failure:^(NSError *error) {
        IWLog(@"%@",error);
        [MBProgressHUD showError:@"无法连接服务器"];
        return ;
    }];

}
/**
 *  登录
 */
- (void)login{
    //判断是否满足登录的条件
    if([self.accountField.text isEqualToString:@""]){
        [MBProgressHUD showError:@"帐号不能为空"];
        return;
    }else if([self.passwordField.text isEqualToString:@""]){
        [MBProgressHUD showError:@"密码不能为空"];
        return;
    }else if ([self.chooseCommunity.text isEqualToString:@"请选择小区(点我)"]){
        [MBProgressHUD showError:@"请选择小区"];
        return;
    }
    //向服务器请求数据
    NSString *urlString = [APIClient relativePathForLogin];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user"] = self.accountField.text;
    params[@"pwd"] = self.passwordField.text;
    params[@"place"] = self.dbName;
    params[@"role"] = @1;
    [PPHttpTool postWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
        if ([json[@"resultCode"] isEqual:@1]) {
            [MBProgressHUD showSuccess:@"登录成功"];
            //获取用户信息
            NSString *urlString = [APIClient relativePathForUserInfo];
            [PPHttpTool getWithURL:urlString params:nil success:^(id json) {
                IWLog(@"%@",json);
                self.userInfo = (UserInfo *)[UserInfo objectWithKeyValues:json];
                self.userInfo.dbName = self.dbName;
                self.userInfo.community = self.communityName;
                self.userInfo.account = params[@"user"];
                self.userInfo.password = params[@"pwd"];
                self.userInfo.isRemember = self.isRemember;
                //保存用户数据模型
                [[EGOCache globalCache] setObject:self.userInfo forKey:Cache_UserInfoKey withTimeoutInterval:CacheTime];
            } failure:^(NSError *error) {
                
            }];
            PPTabBarController *tabBarVC = [[PPTabBarController alloc] init];
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            window.rootViewController = tabBarVC;
            NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
            NSData *cookiesData = nil;
            for (NSHTTPCookie *cookie in [cookieJar cookies]) {
                NSLog(@"cookie %@",cookie);
                //cookiesArray = [[NSArray alloc] initWithObjects:cookie, nil];
                cookiesData = [NSKeyedArchiver archivedDataWithRootObject:cookie];
            }
        }else if([json[@"resultCode"] isEqual:@0]){
            [MBProgressHUD showError:@"帐号或密码错误"];
        }else if([json[@"resultCode"] isEqual:@999]) {
            [Tool HUDShowMessage:@"找不到该用户"];
        }
    } failure:^(NSError *error) {
        IWLog(@"%@",error);
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"操作失败"];
    }];
}
/**
 *  显示协议
 */
- (void)showProtocol
{
    
}
/**
 *  隐藏键盘
 */
- (void)hidenKeyboard
{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
